package com.online_exam.service.impl;

import com.online_exam.base.UserType;
import com.online_exam.domain.other.KeyValue;
import com.online_exam.repository.UserMapper;
import com.online_exam.domain.User;
import com.online_exam.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public User selectByUsernamePassword(String username, String password) {
        Map map = new HashMap();
        map.put("userName", username);
        map.put("password", password);
        return userMapper.selectByUsernamePassword(map);
    }

    @Override
    public int InsertUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public User selectByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public int update(User user) {
        return userMapper.update(user);
    }



    //根据枚举用户类型获取cookie中的用户名 -不同用户类型存入的cookie名不同
    @Override
    public User getCurrent(UserType userType, Cookie[] cookies) {
        String username;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(userType.getType())){
                username=cookie.getValue();
                System.out.println("当前用户："+username);
                User user = selectByUsername(username);
                System.out.println(user);
                return user;
            }
        }
        return null;
    }

    @Override
    public User findByUserId(Integer id) {
        return userMapper.findByUserId(id);
    }

    @Override
    public List<KeyValue> findByUserName(String username) {

        List<KeyValue> list = userMapper.findByUserName(username);
        return list;
    }
}
